package com.ruoyi.web.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Bus;
import com.ruoyi.system.service.IBusService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆Controller
 * 
 * @author ruoyi
 * @date 2021-01-30
 */
@Controller
@RequestMapping("/system/bus")
public class BusController extends BaseController
{
    private String prefix = "system/bus";

    @Autowired
    private IBusService busService;

    @RequiresPermissions("system:bus:view")
    @GetMapping()
    public String bus()
    {
        return prefix + "/bus";
    }

    /**
     * 查询车辆列表
     */
    @RequiresPermissions("system:bus:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Bus bus)
    {
        startPage();
        List<Bus> list = busService.selectBusList(bus);
        return getDataTable(list);
    }

    /**
     * 导出车辆列表
     */
    @RequiresPermissions("system:bus:export")
    @Log(title = "车辆", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Bus bus)
    {
        List<Bus> list = busService.selectBusList(bus);
        ExcelUtil<Bus> util = new ExcelUtil<Bus>(Bus.class);
        return util.exportExcel(list, "bus");
    }

    /**
     * 新增车辆
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存车辆
     */
    @RequiresPermissions("system:bus:add")
    @Log(title = "车辆", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Bus bus)
    {
        return toAjax(busService.insertBus(bus));
    }

    /**
     * 修改车辆
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Bus bus = busService.selectBusById(id);
        mmap.put("bus", bus);
        return prefix + "/edit";
    }

    /**
     * 修改保存车辆
     */
    @RequiresPermissions("system:bus:edit")
    @Log(title = "车辆", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Bus bus)
    {
        return toAjax(busService.updateBus(bus));
    }

    /**
     * 删除车辆
     */
    @RequiresPermissions("system:bus:remove")
    @Log(title = "车辆", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(busService.deleteBusByIds(ids));
    }
}
